﻿using CoreShop.Library;
using CoreShop.Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace CoreShop.App
{
    public partial class UIDishPrintbillTemplateItem : UIUserControl
    {
        public UIDishPrintbillTemplateItem()
        {
            InitializeComponent();
        }

        public corecmsprintbilltemplate template { get; set; }
        public UIDishPrintbillTemplateItem(corecmsprintbilltemplate _template,bool isSaveTemplate)
        {
            InitializeComponent();
            template = _template;
            if ((template.billtypeId == (int)GlobalEnumVars.PrintbillType.ClassBill)
                || (template.billtypeId == (int)GlobalEnumVars.PrintbillType.PrePayment)
                || (template.billtypeId == (int)GlobalEnumVars.PrintbillType.FoodList)
                || (template.billtypeId == (int)GlobalEnumVars.PrintbillType.Settlement)
                || (template.billtypeId == (int)GlobalEnumVars.PrintbillType.UserDeposit)
                || (template.billtypeId == (int)GlobalEnumVars.PrintbillType.Label)
                || (template.billtypeId == (int)GlobalEnumVars.PrintbillType.Report))
            {
                this.btnCommadSet.Visible = false;
            } else this.btnCommadSet.Visible = true;
            tsmSaveToPool.Visible = isSaveTemplate;
        }

        public UIButton CreateButton(string text)
        {
            UIButton btn = new UIButton();
            btn.BackColor = System.Drawing.Color.White;
            btn.Cursor = System.Windows.Forms.Cursors.Hand;
            btn.FillColor = System.Drawing.Color.White;
            btn.FillColor2 = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(155)))), ((int)(((byte)(40)))));
            btn.FillHoverColor = System.Drawing.Color.FromArgb(((int)(((byte)(227)))), ((int)(((byte)(175)))), ((int)(((byte)(83)))));
            btn.FillPressColor = System.Drawing.Color.FromArgb(((int)(((byte)(176)))), ((int)(((byte)(124)))), ((int)(((byte)(32)))));
            btn.FillSelectedColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(248)))), ((int)(((byte)(229)))));
            btn.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            btn.ForeColor = System.Drawing.Color.Black;
            btn.ForeHoverColor = System.Drawing.Color.Black;
            btn.ForePressColor = System.Drawing.Color.Black;
            btn.ForeSelectedColor = System.Drawing.Color.Black;
            btn.MinimumSize = new System.Drawing.Size(1, 1);
            btn.Name = text;
            btn.Radius = 9;
            btn.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
            btn.RectHoverColor = System.Drawing.Color.FromArgb(((int)(((byte)(227)))), ((int)(((byte)(175)))), ((int)(((byte)(83)))));
            btn.RectPressColor = System.Drawing.Color.FromArgb(((int)(((byte)(176)))), ((int)(((byte)(124)))), ((int)(((byte)(32)))));
            btn.RectSelectedColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(189)))), ((int)(((byte)(0)))));
            btn.Size = new System.Drawing.Size(58, 25);
            btn.Style = Sunny.UI.UIStyle.Custom;
            btn.StyleCustomMode = true;
            btn.Text = text;
            btn.Parent = this;
            return btn;
        }

        private void UIFDishPinterSetupItem_Paint(object sender, PaintEventArgs e)
        {
            if (Selected)
            {
                e.Graphics.FillRectangle(SelectedColor, 0, 0, Width, Height);
            }
            if (template == null) return;
            using (Font font = new System.Drawing.Font("微软雅黑", 10.5F))
            {
                e.Graphics.DrawString(template.name, font, Color.Black, new Rectangle(30, 0, Width, Height), ContentAlignment.MiddleLeft);
                e.Graphics.DrawString(EnumHelper.GetDescByValue<GlobalEnumVars.paperType>(template.paperType), font, Color.Black, new Rectangle(220, 0, Width, Height), ContentAlignment.MiddleLeft);
                e.Graphics.DrawString(EnumHelper.GetDescByValue<GlobalEnumVars.foodPrintType>(template.foodPrintType), font, Color.Black, new Rectangle(330, 0, Width, Height), ContentAlignment.MiddleLeft);
            }
        }

        public event EventHandler ModifyItemClick;
        private void btn_Click(object sender, EventArgs e)
        {
            ModifyItemClick?.Invoke(template, e);
        }

        

        public event EventHandler ModifyReportClick;
        private void btnRpt_Click(object sender, EventArgs e)
        {
            ModifyReportClick?.Invoke(template, e);
        }

        

        private bool selected = false;
        public bool Selected
        {
            get => selected;
            set
            {
                selected = value;
                Invalidate();
            }
        }

        private Color selectedColor = Color.FromArgb(221, 221, 221);
        public Color SelectedColor
        {
            get => selectedColor;
            set
            {
                selectedColor = value;
                Invalidate();
            }
        }

        public event EventHandler TableClick;

        private void UIDishPrintbillTemplateItem_Click(object sender, System.EventArgs e)
        {
            //Selected = !selected;
            Selected = true;
            foreach (var item in Parent.GetControls<UIDishPrintbillTemplateItem>())
            {
                if (item.Equals(this))
                {
                    item.btnCommadSet.BackColor = selectedColor;
                    item.btnRpt.BackColor = selectedColor;                     
                    continue;
                }

                item.Selected = false;
                item.btnCommadSet.BackColor = item.BackColor;
                item.btnRpt.BackColor = item.BackColor;
            }
            TableClick?.Invoke(this, e);
        }

        private void btnMore_Click(object sender, EventArgs e)
        {
            btnMore.ShowContextMenuStrip(cmsMenu, 0, btnMore.Height);
        }

        public event EventHandler CopyItemClick;
        private void tsmCopyFromPool_Click(object sender, EventArgs e)
        {
            CopyItemClick?.Invoke(template, e);
        }

        public event EventHandler DeleteItemClick;
        private void tsmDelete_Click(object sender, EventArgs e)
        {
            DeleteItemClick?.Invoke(template, e);
        }

        public event EventHandler SaveItemClick;
        private void tsmSaveToPool_Click(object sender, EventArgs e)
        {
            SaveItemClick?.Invoke(template, e);
        }
    }
}
